<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Lists extends CI_Controller
{
	
	function __construct()
	{
		parent::__construct();
	}
	
	function index()
	{
		
		$olddate = 0;
		$oldsid = 0;
		$this->load->library("session_manager");
		$sm = $this->session_manager;
		$this->load->database();
		$this->load->model("Gps_log_list");
		
		$data = array(
			"title" => "List - Track List",
			"jsarray" => array("./resources/js/list.js"),
			"extra" => ''
		);
		
		//输出HTML表单
		$this->load->view("header", $data);

		$data = array("nickname" => $sm->user['nickname']);
		$this->load->view("menu", $data);
		
		
		
		$list_html = "";
		$row = "";
		
		$list = $this->Gps_log_list->get_list($sm->user['uid']);
		if ($list) //存在列表
		{
			foreach ( $list as $item )
			{
				$d = floor(($item["src_gen_time"] + TIME_OFFSET) / 86400)*86400; //取日期，去除时间
				if ($olddate != $d) //日期不同，显示日期标识
				{
					if ($olddate > 0) //显示上一日期的所有内容
					{
						$data = array("date" => $olddate, "row"=> $row);
						$list_html .= $this->load->view("list_index_date", $data, true);
					}

					$olddate = $d;
					$row = "";
				}
				if ($oldsid != $item["sid"]) //source file不同，SID不同分段显示
				{
					$oldsid = $item["sid"];
					$data = array("sid" => $item["sid"],
						      "source_name" => $item["source_desc"],
						      "source_file" => $this->_build_path($sm->user['uid'],$item["upload_time"],"src")."/".$item["source_file"],
						      "upload_time" => @date("Y-m-d H:i:s", $item["upload_time"] + TIME_OFFSET));
					$row .= $this->load->view("list_index_source", $data, true);				
				}
				
				//添加每条轨迹的信息
				$data = array("description" => addslashes($item["description"]),
					      "shortname" => $item["shortname"],
					      "fid" => $item["fid"],
					      //"uid" => $sm->user["uid"],
					      "kmlfile" => $this->_build_path($sm->user["uid"],$item["gen_time"],"kml")."/".$item["filename"].".kml",
					      "start" => @date("Y-m-d H:i:s", $item["start"] + TIME_OFFSET),
					      "end" => @date("Y-m-d H:i:s", $item["end"] + TIME_OFFSET));
				$row .= $this->load->view("list_index_track", $data, true);				
			}
			
		}

		if ($row) //最后一行
		{
			$data = array("date" => $d, "row"=> $row);
			$list_html .= $this->load->view("list_index_date", $data, true);
		}
		
		$this->load->view("lists", array("lists" => $list_html, "selected" => 0));
		
		$this->load->view("footer");
		

	}
		
	function unparsed()
	{
		$olddate = 0;
		$oldsid = 0;
		$this->load->library("session_manager");
		$sm = $this->session_manager;
		$this->load->database();
		$this->load->model("Gps_log_source");
		
		$data = array(
			"title" => "List - Unparsed Source List",
			"jsarray" => array("./resources/js/list.js"),
			"extra" => ''
		);
		
		//输出HTML表单
		$this->load->view("header", $data);

		$data = array("nickname" => $sm->user['nickname']);
		$this->load->view("menu", $data);

		$list_html = "";
		$row = "";
		
		$list = $this->Gps_log_source->get_unparsed_list($sm->user['uid']);
		if ($list) //存在列表
		{
			foreach ( $list as $item )
			{
				$d = floor(($item["gen_time"] + TIME_OFFSET) / 86400)*86400; //取日期，去除时间
				if ($olddate != $d) //日期不同，显示日期标识
				{
					if ($olddate > 0) //显示上一日期的所有内容
					{
						$data = array("date" => $olddate, "row"=> $row);
						$list_html .= $this->load->view("list_index_date", $data, true);
					}

					$olddate = $d;
					$row = "";
				}
				if ($oldsid != $item["sid"]) //source file不同，SID不同分段显示
				{
					$oldsid = $item["sid"];
					$data = array("sid" => $item["sid"],
						      "source_name" => $item["description"],
						      "source_file" => $this->_build_path($sm->user['uid'],$item["upload_time"],"src")."/".$item["filename"],
						      "upload_time" => @date("Y-m-d H:i:s", $item["upload_time"] + TIME_OFFSET));
					$row .= $this->load->view("list_index_source", $data, true);				
				}
							
			}
			
		}

		if ($row) //最后一行
		{
			$data = array("date" => $d, "row"=> $row);
			$list_html .= $this->load->view("list_index_date", $data, true);
		}
		
		$this->load->view("lists", array("lists" => $list_html, "selected" => 1));
		
		$this->load->view("footer");		
	}

	function _build_path($uid,$timestamp,$type,$url=true)
	{
		$dirs = array(
			$uid,
			$type,
			@date("Y", $timestamp),
			@date("m", $timestamp),
			@date("d", $timestamp)
		);
		
		if ($url) //生成URL或实际路径
		    $path = base_url().STORAGE_PATH;
		else
		    $path = base_dir().STORAGE_PATH;
		foreach ($dirs as $dir){
			$path .="/".$dir;
		}
				
		return $path;
	}

	
}
?>